/**
 * 栈实现
 *
 */

class Stack {
  
  constructor() {
    this.item = []  
  }
  
  push(){
    this.item.push(...arguments);
  }
  
  pop(){
    return this.item.pop()  
  }

  peek(){
    return this.item[this.item.length - 1] 
  }
  
  isEmpty(){
    return this.item.length === 0  
  }
  
  clear(){
    this.item = []
  }  
   
  print(){
    console.log(JSON.stringify(this.item))  
  }
}
/*
const stack = new Stack()

stack.push(1, 2, 3, 4, 5, 6)
stack.peek()
stack.isEmpty()
stack.pop()
stack.print()
*/

/**
 * 队列实现
 *
 */

class Queue {
  constructor(){
    this.item = [];  
  }
  
  enqueue(){
    this.item.push(...arguments)  
  } 
  
  dequeue(){
    return this.item.shift()  
  }  
  
  first(){
    return this.item[0]  
  }

  isEmpty(){
    return this.item.length === 0;  
  }

  size(){
    return this.item.length  
  }
  
  clear(){
    this.item = []  
  }

  print(){
    console.log(JSON.stringify(this.item))  
  }
}

const queue = new Queue();

queue.enqueue(1, 2, 3, 4, 5, 6)
queue.dequeue()
queue.first()
queue.print()

/*
 * 优先队列
 */
class PriorityQueue {
 
  constructor(element, priority){
    this.items = []
    this.element = element
    this.priority = priority
  }

  enQueue(element, priority) {
    const queObj = new PriorityQueue(element, priority)
    if(this.isEmpty()) {
      this.push(queObj)  
    } else {
      let bAdded = false
      for(let i = 0, len = this.items.length; i < len; i++) {
        if(priority < this.items[i].priority){
          this.items.splice(i, 0, queObj)
          bAdded = true
          break  
        }
      }
      if(!bAdded) this.items.push(queObj) 
    }
  }

  deQueue() {
    return this.items.shift()  
  }

  front() {
    return this.items[0]  
  }

  isEmpty() {
    return this.items.length === 0  
  }

  size() {
    return this.items.length  
  }

  clear() {
    this.items = []  
  }

  print(){
    const temp = []
    for(let i = 0, len = this.items.length; i < len; i++) {
      temp.push(this.items[i].element)  
    }
    console.log(temp.toString())
  }
}

const priorityQueue = new PriorityQueue(12, 12)

console.log(priorityQueue)
